package com.mindmax.user.service.impl;

import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mindmax.user.domain.AdminUser;
import com.mindmax.user.domain.TenantUser;
import com.mindmax.user.mapper.AdminUserMapper;
import com.mindmax.user.mapper.TenantUserMapper;
import com.mindmax.user.service.IAdminUserService;
import com.mindmax.user.vo.BindingTenantUserVo;
import com.mmb.crypt.config.BCryptUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 忆图用户Service业务层处理
 *
 * @author cabloy
 * @date 2024-04-02
 */
@Slf4j
@Service
public class AdminUserServiceImpl implements IAdminUserService
{
    @Autowired
    private AdminUserMapper adminUserMapper;


    @Autowired
    private TenantUserMapper tenantUserMapper;
    /**
     * 查询忆图用户
     *
     * @param id 忆图用户主键
     * @return 忆图用户
     */
    @Override
    public AdminUser selectAdminUserById(Long id)
    {
        return adminUserMapper.selectAdminUserById(id);
    }

    /**
     * 查询忆图用户列表
     *
     * @param adminUser 忆图用户
     * @return 忆图用户
     */
    @Override
    public List<AdminUser> selectAdminUserList(AdminUser adminUser)
    {
        return adminUserMapper.selectAdminUserList(adminUser);
    }

    /**
     * 新增忆图用户
     *
     * @param adminUser 忆图用户
     * @return 结果
     */
    @Override
    public int insertAdminUser(AdminUser adminUser)
    {
        adminUser.setCreateTime(DateUtils.getNowDate());
        String password = adminUser.getPassword();
        String crypted        = BCryptUtil.genHashPassword(adminUser.getPassword());
        log.info("password plaint is {}, crypted is:{}",password, crypted);
        adminUser.setPassword(crypted);
        return adminUserMapper.insertAdminUser(adminUser);
    }

    /**
     * 修改忆图用户
     *
     * @param adminUser 忆图用户
     * @return 结果
     */
    @Override
    public int updateAdminUser(AdminUser adminUser)
    {
        adminUser.setUpdateTime(DateUtils.getNowDate());
        AdminUser preAdminUser = adminUserMapper.selectAdminUserById(adminUser.getId());
        if (!StringUtils.equals(preAdminUser.getPassword(), adminUser.getPassword())) {
            adminUser.setPassword(BCryptUtil.genHashPassword(adminUser.getPassword()));
        }
        return adminUserMapper.updateAdminUser(adminUser);
    }

    /**
     * 批量删除忆图用户
     *
     * @param ids 需要删除的忆图用户主键
     * @return 结果
     */
    @Override
    public int deleteAdminUserByIds(Long[] ids)
    {
        return adminUserMapper.deleteAdminUserByIds(ids);
    }

    /**
     * 删除忆图用户信息
     *
     * @param id 忆图用户主键
     * @return 结果
     */
    @Override
    public int deleteAdminUserById(Long id)
    {
        return adminUserMapper.deleteAdminUserById(id);
    }

    @Override
    public List<AdminUser> selectAdminUserByIds(List<Long> userIds) {
        if (CollectionUtils.isEmpty(userIds)) {
            return Lists.newArrayList();
        }
        return adminUserMapper.selectAdminUserByIds(userIds);
    }

    @Override
    public Map<Long, Integer> selectCountByTenant(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Maps.newHashMap();
        }
        return adminUserMapper.selectCountByTenant(ids);
    }

    @Override
    public void rebindingUserTenant(BindingTenantUserVo bindingTenantUserVo) {
        Long userId = bindingTenantUserVo.getUserId();
//        tenantUserMapper.deleteTenantUserByUserId(userId);
        String[] tenantIds = bindingTenantUserVo.getTenantIds();
        //3,5,6 => list.of(3,5,6)
        List<Long> tenantIdList = Lists.newArrayList();
        for (String tenantId : tenantIds) {
            tenantIdList.add(Long.valueOf(tenantId));
        }
        if (org.springframework.util.CollectionUtils.isEmpty(tenantIdList)) {
            return;
        }
        for (Long tenantId : tenantIdList) {
            TenantUser tenantUser = new TenantUser();
            tenantUser.setUserId(userId);
            tenantUser.setTenantId(tenantId);;
            try {
                tenantUserMapper.insertTenantUser(tenantUser);
            } catch (Exception e) {
                System.out.println("e = " + e);
            }
        }
    }
}
